/*
*    Clean AJAX Engine v4.0
*    Copyright (C) 2005-2006 Carlos Eduardo Goncalves (cadu.goncalves@gmail.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program; if not, write to the Free Software
*    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
* Provides a simple queue to add messages in.
* @author Carlos Eduardo Goncalves
*/

/** 
* <p>AJAX message queue constructor.</p>
*/
function MessageQueue(){
}

/** <p>Array that holds all active messages.</p> */
MessageQueue.messages = new Array();		

/** 
* <p>Add a new AJAX message to the queue.</p>
* @param msg
*		<code>Message</code> value object to be added.	
* @return
*		<code>MessageWrapper</code> instance stored on the queue.
*/
MessageQueue.add = function(msg){ 
  try{
    var wrapper = new MessageWrapper();
    wrapper.wrap(msg);
    MessageQueue.messages.push(wrapper);	 
    return wrapper;
  } catch(e){Engine.reportException(MessageQueue.messages.length, e);}
}

/** 
* <p>Remove an AJAX message from the queue.</p>
* @param msg_id
*		<code>Message<code> unique identifier on the message queue. 		
*/
MessageQueue.remove = function(msg_id){
  if(isNaN(msg_id)) 
    return;
  if(MessageQueue.messages.length > msg_id)
    MessageQueue.messages[msg_id] = null;
}

/** 
* <p>Method used to safe access messages array and recover
* a message wrapper.</p>
* @param msg_id
*		<code>Message</code> unique identifier on the message queue.	
* @return 
*		<code>MessageWrapper</code> instance or <code>null</code>.
*/
MessageQueue.getMessage = function(msg_id){
  if(isNaN(msg_id)) 
    return null;
  if(MessageQueue.messages.length > msg_id)
    return MessageQueue.messages[msg_id];
  else
    return null;
}

/** 
* <p>Method used to clear message queue.</p>
*/
MessageQueue.clear = function(){	
  var empty = true;
  for(var i = 0; i < MessageQueue.messages.length; i++) {
    if(MessageQueue.messages[i] != null){
	  var wrapper = MessageQueue.messages[i];
	  if(wrapper.request.readyState < 4){
	    empty = false;
	    break;
	  }
	}
  }
  if(empty)
    MessageQueue.messages = new Array();
}

/** 
* <p>Check if the queue has any active message.</p>
* <p>Silent messages are ignored.</p>
* @return 
*		<code>Boolean</code> flag that indicates if the are active messages.
*/
MessageQueue.hasActiveMessage = function(){	
  var active = false;
  for(var i = 0; i < MessageQueue.messages.length; i++) {
    if(MessageQueue.messages[i] != null){
	  var wrapper = MessageQueue.messages[i];
	  if((wrapper.request.readyState < 4) && (wrapper.silent == false)){
	    active = true;
	    break;
	  }
	}
  }
  return active;
}